package com.jeesite.modules.storage.util;

import com.jeesite.common.config.Global;
import com.jeesite.modules.storage.common.Model.HxRegisterUserModel;
import com.jeesite.modules.storage.common.Model.HxRegisterUserResponseModel;
import com.jeesite.modules.storage.common.Model.HxTokenRequestModel;
import com.jeesite.modules.storage.common.Model.HxTokenResponseModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HXUtil {

    public static String url = Global.getConfig("storage.hxpush.host");
    private static String appKey = Global.getConfig("storage.hxpush.appkey");
    private static String clientId = Global.getConfig("storage.hxpush.clientId");
    private static String clientSecret = Global.getConfig("storage.hxpush.clientSecret");


    public static HxTokenResponseModel GetToken() {
        HxTokenRequestModel model = new HxTokenRequestModel();
        model.setGrant_type("client_credentials");
        model.setClient_id(clientId);
        model.setClient_secret(clientSecret);
        String dataStr = JSON.toJSONString(model);
        String org_name = appKey.split("#")[0];
        String app_name = appKey.split("#")[1];
        String apiUrl = url + org_name + "/" + app_name + "/token";
        String result = postJson(dataStr, apiUrl, null);
        HxTokenResponseModel response = (HxTokenResponseModel) JSON.parse(result);
        return response;
    }

    /**
     * 开放注册 token为null
     * 授权注册 token要传值
     * 批量注册
     */
    public static HxRegisterUserResponseModel RegisterUser(List<HxRegisterUserModel> userModelList, String token) {

        String dataStr = JSON.toJSONString(userModelList);

        Map<String, String> header = new HashMap<>();
        if (StringUtils.isBlank(token))
            header.put("Authorization", "Bearer " + token);

        String org_name = appKey.split("#")[0];
        String app_name = appKey.split("#")[1];
        String apiUrl = url + org_name + "/" + app_name + "/users";

        String result = postJson(dataStr, apiUrl, header);
        HxRegisterUserResponseModel response = (HxRegisterUserResponseModel) JSON.parse(result);
        return response;
    }


    public static String postJson(String dataStr, String url, Map<String, String> header) {
        String result = "";
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            BasicResponseHandler handler = new BasicResponseHandler();
            if (null != header && header.size() > 0) {
                for (Map.Entry<String, String> item : header.entrySet()) {
                    httpPost.addHeader(item.getKey(), item.getValue());
                }
            }
            StringEntity entity = new StringEntity(dataStr, "utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            result = httpClient.execute(httpPost, handler);
            return result;
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
